package srx.open.com.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baidu.location.BDLocation;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobGeoPoint;
import cn.bmob.v3.listener.FindListener;
import de.greenrobot.event.EventBus;
import srx.open.com.R;
import srx.open.com.activity.CourseDetailedInfoActivity;
import srx.open.com.activity.PublishCourseActivity;
import srx.open.com.adapter.ClassRecyclerAdapter;
import srx.open.com.adapter.NearbyInfoAdpter;
import srx.open.com.bean.CoursePublish;
import srx.open.com.bean.User;
import srx.open.com.mode.ClassItem;
import srx.open.com.utils.Constants;
import srx.open.com.utils.Log;
import srx.open.com.utils.OpenToast;
import srx.open.com.utils.UserUtils;
import srx.open.com.views.OnRefreshScrollHeadListener;
import srx.open.com.views.RefreshListView;

/**
 * 附近fragment模块.
 *
 * @author hailongqiu 356752238@qq.com
 */
public class NearbyFragment extends BaseFragment {

    private static final String TAG = "NearbyFragment";

    private static final double MENU_RECOMMEND_FLAGS = 1.0f;
    private static final double MENU_TOP_FLAGS = 2.0f;
    private static final double MENU_NEW_FLAGS = 3.0f;

    public static NearbyFragment newInstance(String s) {
        NearbyFragment nearbyFragment = new NearbyFragment();
        return nearbyFragment;
    }

    // 类型.
    private List<ClassItem> mTypeitems = new ArrayList<ClassItem>() {
        {
            add(new ClassItem("全部", -1));
            add(new ClassItem("教学生", PublishCourseActivity.TEACH_STUDENT_STATE));
            add(new ClassItem("寻老师", PublishCourseActivity.FIND_TEACHER_STATE));
        }
    };

    // 排序.
    private List<ClassItem> mTopitems = new ArrayList<ClassItem>() {
        {
            add(new ClassItem("最新", MENU_NEW_FLAGS));
            add(new ClassItem("推荐", MENU_RECOMMEND_FLAGS));
            add(new ClassItem("热门", MENU_TOP_FLAGS));
        }
    };

    // 分类
    private List<ClassItem> mClassItems = new ArrayList<ClassItem>();
    private List<ClassItem> mNextClassItems = new ArrayList<ClassItem>();

    // 距离.
    private List<ClassItem> mKmItems = new ArrayList<ClassItem>() {
        {
            add(new ClassItem("500m", 0.5f));
            add(new ClassItem("1km", 1.0f));
            add(new ClassItem("2km", 2.0f));
            add(new ClassItem("5km", 5.0f));
        }
    };

    // 标题栏
    @Bind(R.id.head_location_iv)
    ImageView head_location_iv;
    @Bind(R.id.head_point_tv)
    TextView head_point_tv;
    @Bind(R.id.home_head_city_tv)
    TextView home_head_city_tv;
    @Bind(R.id.home_head_baddr_tv)
    TextView home_head_baddr_tv;
    @Bind(R.id.head_arrows_iv)
    ImageView head_arrows_iv;

    // 类型菜单
    RecyclerView type_recyclearview;
    // 排序菜单.
    RecyclerView top_recyclearview;
    // 分类菜单.
    RecyclerView class_recyclearview;
    // 分类的下一级菜单.
    RecyclerView class_next_recyclearview;
    // 距离菜单.
    RecyclerView km_recyclearview;

    //
    @Bind(R.id.pull_refresh_list)
    RefreshListView mPullRefreshListView;
    @Bind(R.id.nearby_progress)
    ProgressBar mProgressBar;

    List<CoursePublish> mPubList = new ArrayList<CoursePublish>();
    NearbyInfoAdpter mNearbyInfoAdpter;
    SearchDataMode mSearchDataMode; // 搜索的数据集

    @Bind(R.id.add_flayt)
    FrameLayout add_flayt;
    @Bind(R.id.home_select_show_lay)
    LinearLayout home_select_show_lay;
    // 类型.
    @Bind(R.id.type_show_tv)
    TextView type_show_tv;
    // 排序
    @Bind(R.id.sort_show_tv)
    TextView sort_show_tv;
    // 分类
    @Bind(R.id.class_show_tv)
    TextView class_show_tv;
    // 下一个分类.
    @Bind(R.id.next_class_show_tv)
    TextView next_class_show_tv;
    // 距离.
    @Bind(R.id.km_radio_show_tv)
    TextView km_radio_show_tv;

    //
    ClassRecyclerAdapter mTypeRecyclerAdapter;
    ClassRecyclerAdapter mTopRecyclerAdapter;
    ClassRecyclerAdapter mClassRecyclerAdapter;
    ClassRecyclerAdapter mClassNextRecyclerAdapter;
    ClassRecyclerAdapter mKmRecyclerAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_nearby, container, false);
        ButterKnife.bind(this, view);
        initAllViews();
        return view;
    }

    @Override
    public void initAllDatas() {
        // 初始化二级分类菜单.
        mClassItems = mDataBaseHelper.queryClassifyTwo(-1);
        mClassItems.add(0, null);
        // 初始化三级分类菜单.
//        if (mClassItems != null && mClassItems.size() > 0) {
//            ClassItem classItem = mClassItems.get(1);
//            mNextClassItems = mDataBaseHelper.queryClassifyThree(classItem.getTwoId());
//            mNextClassItems.add(0, null);
//        }
        // 初始化 adapter.
        mTypeRecyclerAdapter = new ClassRecyclerAdapter(mTypeitems);
        mTopRecyclerAdapter = new ClassRecyclerAdapter(mTopitems);
        mClassRecyclerAdapter = new ClassRecyclerAdapter(mClassItems);
        mClassNextRecyclerAdapter = new ClassRecyclerAdapter(mNextClassItems);
        mKmRecyclerAdapter = new ClassRecyclerAdapter(mKmItems);
        //
        Log.i(TAG, "initAllDatas start===");
        EventBus.getDefault().register(this); // 注册事件.
        mSearchDataMode = new SearchDataMode();
        /* 启动百度定位 */
        mBaiDuLbsHelper.mLocationClient.start();
        Log.i(TAG, "initAllDatas end===");
    }

    LinearLayoutManager linearLayoutManager;

    private void initAllViews() {
        initTitles();
        //
        setHomeSelectShowBtnText();
        // 添加头信息.
        final View mHeadView = View.inflate(mContext, R.layout.include_nearby_top_tool,
                null);
        type_recyclearview = (RecyclerView) mHeadView.findViewById(R.id.type_recyclearview);
        top_recyclearview = (RecyclerView) mHeadView.findViewById(R.id.top_recyclearview);
        class_recyclearview = (RecyclerView) mHeadView.findViewById(R.id.class_recyclearview);
        class_next_recyclearview = (RecyclerView) mHeadView.findViewById(R.id.class_next_recyclearview);
        km_recyclearview = (RecyclerView) mHeadView.findViewById(R.id.km_recyclearview);
        mPullRefreshListView.addHeaderView(mHeadView, null, false);
        // 头菜单的显示和隐藏.
        mPullRefreshListView.setOnRefreshScrollHeadListener(new OnRefreshScrollHeadListener() {
            @Override
            public void onCanScrollUpHead(RefreshListView view) {
                home_select_show_lay.setVisibility(View.VISIBLE);
            }

            @Override
            public void onCanScrollDownHead(RefreshListView view) {
                home_select_show_lay.setVisibility(View.GONE);
            }

            @Override
            public void onScrollMove(RefreshListView view) {
                if (mPullRefreshListView.getHeaderViewsCount() == 1) {
                    // 还原筛选菜单显示在listview的headview上.
                    add_flayt.removeView(mHeadView);
                    ListView.LayoutParams params = new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, mHeadView.getMeasuredHeight());
                    mHeadView.setLayoutParams(params);
                    mPullRefreshListView.addHeaderView(mHeadView);
                }
            }

        });
        //
        home_select_show_lay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPullRefreshListView.getHeaderViewsCount() == 2) {
                    // 显示筛选菜单.
                    add_flayt.addView(mHeadView);
                    mPullRefreshListView.removeHeaderView(mHeadView);
                }
            }
        });
        //
        initTypeRecyclearView();
        initTopRecyclearView();
        initClassRecyclearView();
        initClassNextRecyclearView();
        initKmRecyclearView();
        initPullRefreshListView();
    }

    private void initTitles() {
        head_location_iv.setVisibility(View.GONE);
        head_point_tv.setVisibility(View.GONE);
        home_head_baddr_tv.setVisibility(View.GONE);
        head_arrows_iv.setVisibility(View.GONE);
        home_head_city_tv.setText("附近");
    }

    private void setHomeSelectShowBtnText() {
        // 类型
        if (!TextUtils.isEmpty(mSearchDataMode.typeText)) {
            type_show_tv.setVisibility(View.VISIBLE);
            type_show_tv.setText(mSearchDataMode.typeText);
        } else {
            type_show_tv.setVisibility(View.GONE);
        }
        // 排序
        if (!TextUtils.isEmpty(mSearchDataMode.sortText)) {
            sort_show_tv.setVisibility(View.VISIBLE);
            sort_show_tv.setText(mSearchDataMode.sortText);
        } else {
            sort_show_tv.setVisibility(View.GONE);
        }
//        if (TextUtils.isEmpty(mSearchDataMode.oneText))
//            text += mSearchDataMode.oneText;
        // 分类.
        if (!TextUtils.isEmpty(mSearchDataMode.twoText)) {
            class_show_tv.setVisibility(View.VISIBLE);
            class_show_tv.setText(mSearchDataMode.twoText);
        } else {
            class_show_tv.setVisibility(View.GONE);
        }
        // 下一个分类.
        if (!TextUtils.isEmpty(mSearchDataMode.threeText)) {
            next_class_show_tv.setVisibility(View.VISIBLE);
            next_class_show_tv.setText(mSearchDataMode.threeText);
        } else {
            next_class_show_tv.setVisibility(View.GONE);
        }
        // 距离.
        if (!TextUtils.isEmpty(mSearchDataMode.kmText)) {
            km_radio_show_tv.setVisibility(View.VISIBLE);
            km_radio_show_tv.setText(mSearchDataMode.kmText);
        } else {
            km_radio_show_tv.setVisibility(View.GONE);
            km_radio_show_tv.setText(mSearchDataMode.kmText);
        }
        // 工具条设置筛选的文本信息.
    }

    /**
     * 类型:  教学生 | 寻老师
     */
    private void initTypeRecyclearView() {
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        type_recyclearview.setLayoutManager(linearLayoutManager);
        mTypeRecyclerAdapter.setOnItemCheckListener(new ClassRecyclerAdapter.OnItremCheckListener() {
            @Override
            public void onItemCheck(View v, int pos) {
                // 更新工具条.
                ClassItem classItem = mTypeitems.get(pos);
                mSearchDataMode.typeState = classItem.getTypeState();
                restartUpdateData(DOWN_PULL_STATE_UPDAE); // 刷新课程数据.
                //
                mSearchDataMode.typeText = classItem.getDescription();
                setHomeSelectShowBtnText();
            }
        });
        type_recyclearview.setAdapter(mTypeRecyclerAdapter);
    }

    /**
     * 排序:  推荐 | 热门 | 最新
     */
    private void initTopRecyclearView() {
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        top_recyclearview.setLayoutManager(linearLayoutManager);
        mTopRecyclerAdapter.setOnItemCheckListener(new ClassRecyclerAdapter.OnItremCheckListener() {
            @Override
            public void onItemCheck(View v, int pos) {
                ClassItem classItem = mTopitems.get(pos);
                mSearchDataMode.sortType = classItem.getkmRadio();
                restartUpdateData(DOWN_PULL_STATE_UPDAE); // 刷新课程数据.
                // 更新工具条.
                mSearchDataMode.sortText = classItem.getDescription();
                setHomeSelectShowBtnText();
            }
        });
        top_recyclearview.setAdapter(mTopRecyclerAdapter);
    }

    /**
     * 二级分类:
     */
    private void initClassRecyclearView() {
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        class_recyclearview.setLayoutManager(linearLayoutManager);
        mClassRecyclerAdapter.setOnItemCheckListener(new ClassRecyclerAdapter.OnItremCheckListener() {
            @Override
            public void onItemCheck(View v, int pos) {
                if (pos != 0) { // 其它的选择.
                    ClassItem classItem = mClassItems.get(pos);
                    mNextClassItems = mDataBaseHelper.queryClassifyThree(classItem.getTwoId());
                    // 初始化搜索数据.
                    mSearchDataMode.oneId = classItem.getOneId();
                    mSearchDataMode.twoId = classItem.getTwoId();
                    mSearchDataMode.threeId = -1;
                    // 更新工具条.
                    mSearchDataMode.twoText = "" + classItem.getDescription();
                    mSearchDataMode.threeText = "";
                    //
                    if (mNextClassItems.size() <= 0) {  // 如果为0，证明已经是子菜单.
                        mNextClassItems.clear();
                        mClassNextRecyclerAdapter.setClassItems(mNextClassItems);
                        setHomeSelectShowBtnText();
                    } else { // 三级菜单初始化.
                        mNextClassItems.add(0, null);
                        mClassNextRecyclerAdapter.setClassItems(mNextClassItems);
                    }
                    setHomeSelectShowBtnText();
                    Log.d("mClassRecyclerAdapter.setOnItemCheckListener11111111111111111:" + classItem.toString() + " mNextClassItems size:" + mNextClassItems.size());
                } else { // 全部.
                    mSearchDataMode.oneId = -1;
                    mSearchDataMode.twoId = -1;
                    mSearchDataMode.threeId = -1;
                    mNextClassItems.clear();
                    mClassNextRecyclerAdapter.setClassItems(mNextClassItems);
                    Log.d("mClassRecyclerAdapter.setOnItemCheckListener11111111111111111:全部");
                    // 更新工具条.
                    mSearchDataMode.threeText = "";
                    mSearchDataMode.twoText = "全部";
                    setHomeSelectShowBtnText();
                }
                restartUpdateData(DOWN_PULL_STATE_UPDAE); // 刷新课程数据.
            }
        });
        class_recyclearview.setAdapter(mClassRecyclerAdapter);
    }

    /**
     * 分类下一级: [三级菜单]
     */
    private void initClassNextRecyclearView() {
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        class_next_recyclearview.setLayoutManager(linearLayoutManager);
        mClassNextRecyclerAdapter.setOnItemCheckListener(new ClassRecyclerAdapter.OnItremCheckListener() {
            @Override
            public void onItemCheck(View v, int pos) {
                if (pos != 0) { // 其它的选择.
                    ClassItem classItem = mNextClassItems.get(pos);
                    mSearchDataMode.threeId = classItem.getThreeId();
                    Log.d("mClassNextRecyclerAdapter.setOnItemCheckListener11111111111111111:" + classItem.toString());
                    // 更新工具条.
                    mSearchDataMode.threeText = classItem.getDescription();
                    setHomeSelectShowBtnText();
                } else { // 全部.
                    mSearchDataMode.threeId = -1;
                    // 更新工具条.
                    mSearchDataMode.threeText = "";
                    setHomeSelectShowBtnText();
                    Log.d("mClassNextRecyclerAdapter.setOnItemCheckListener11111111111111111:全部");
                }
                restartUpdateData(DOWN_PULL_STATE_UPDAE);
            }
        });
        class_next_recyclearview.setAdapter(mClassNextRecyclerAdapter);
    }

    /**
     * 距离:  500m 1km 2km 5km.
     */
    private void initKmRecyclearView() {
        linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        km_recyclearview.setLayoutManager(linearLayoutManager);
        mKmRecyclerAdapter.setOnItemCheckListener(new ClassRecyclerAdapter.OnItremCheckListener() {
            @Override
            public void onItemCheck(View v, int pos) {
                ClassItem classItem = mKmItems.get(pos);
                mSearchDataMode.kmRadio = classItem.getkmRadio();
                restartUpdateData(DOWN_PULL_STATE_UPDAE);
                // 更新工具条.
                mSearchDataMode.kmText = classItem.getDescription();
                setHomeSelectShowBtnText();
            }
        });
        km_recyclearview.setAdapter(mKmRecyclerAdapter);
    }

    /*
     * 初始化pullRefresh.
     */
    private void initPullRefreshListView() {
        // 初始化pullrefreshlistview
        mPullRefreshListView.setPageSize(Constants.LIMIT_PAGE_NUM);
        //
        mNearbyInfoAdpter = new NearbyInfoAdpter(mContext, mPubList,
                mDataBaseHelper);
        mPullRefreshListView.setAdapter(mNearbyInfoAdpter);
        // 连接item单击事件.
        mPullRefreshListView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // 查看课程的详细信息.
                CoursePublish coursePublish = mPubList.get(position - 2);
                Log.d(TAG,  "position:" + position + "点击查看课程详情:" + coursePublish.toString());
                Intent intent = new Intent(mContext,
                        CourseDetailedInfoActivity.class);
                intent.putExtra("CoursePublish", coursePublish);
                startActivity(intent);
            }
        });
        // 加载更多.
        mPullRefreshListView.setOnLoadListener(new RefreshListView.OnLoadListener() {
            @Override
            public void onLoad() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        restartUpdateData(UP_PULL_STATE_UPDATE);
                    }
                }).start();
            }
        });
        // 刷新
        mPullRefreshListView.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void toRefresh() {
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(1500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        restartUpdateData(DOWN_PULL_STATE_UPDAE);
                    }
                }).start();
            }
        });
    }

    private void restartUpdateData(final int updateState) {
        queryNearbyCourse(updateState);
    }

    /**
     * 查找附近课程. 116.39727786183,30.78942522(test 地理位置)
     * 要限制查询指定距离范围的数据可以使用addWhereWithinKilometers
     * 、addWhereWithinMiles或addWhereWithinRadians方法。
     * 要查询一个矩形范围内的信息可以使用addWhereWithinGeoBox来实现
     * http://docs.bmob.cn/document/android/index.html
     */
    public void queryNearbyCourse(final int updateState) {
        // 刷新.
        if (updateState == DOWN_PULL_STATE_UPDAE) {
            mSearchDataMode.mSkipPageNum = 0;
        }
        //
        BmobQuery<CoursePublish> bmobQuery = new BmobQuery<CoursePublish>();
        // 类型：教学生 寻老师
        if (mSearchDataMode.typeState != -1) {
            bmobQuery.addWhereEqualTo("type", mSearchDataMode.typeState);
        }
        // 排序 : 最新 推荐 热门
        if (mSearchDataMode.sortType == MENU_NEW_FLAGS) { // 最新.
            bmobQuery.order("-createdAt"); // 创建时间-降序-将最新的排序在最前面.
        } else if (mSearchDataMode.sortType == MENU_RECOMMEND_FLAGS) { // 推荐.
            // 暂时未加入. [-浏览的人最多]
        } else if (mSearchDataMode.sortType == MENU_TOP_FLAGS) { // 热门.
            // 暂时未加入. [-订阅的人最多]
        }
        // 如果分类一为-1. 代表全部.
        // 如果分类二为-1.使用分类一作为基准.
        // 如果分类三为-1.使用分类一，二作为基准.
        // 分类一
        if (mSearchDataMode.oneId != -1) {
            bmobQuery.addWhereEqualTo("oneId", mSearchDataMode.oneId);
            // 分类二
            if (mSearchDataMode.twoId != -1) {
                bmobQuery.addWhereEqualTo("twoId", mSearchDataMode.twoId);
                // 分类三
                if (mSearchDataMode.threeId != -1) {
                    bmobQuery.addWhereEqualTo("threeId", mSearchDataMode.threeId);
                }
            }
        }
        /* 添加课程分类划分条件(添加外部条件，用户 User) */
        BmobQuery<User> innerQuery = new BmobQuery<User>();
        // 排除自己的课程，不显示
        User user = UserUtils.getCurrentUser(mContext);
        if (user != null) {
            innerQuery.addWhereNotEqualTo("username", user.getUsername());
        }
        // 距离过滤.
//        innerQuery.addWhereWithinRadians("location",
        bmobQuery.addWhereWithinRadians("location",
                mSearchDataMode.bmobGeoPoint, mSearchDataMode.kmRadio); //
        Log.w(TAG, "筛选数据:" + mSearchDataMode.toString());
        // 查询一个圆形范围内的课程信息.
//        bmobQuery.addWhereMatchesQuery("usertt", "_User", innerQuery);
        bmobQuery.setLimit(Constants.LIMIT_PAGE_NUM); // 获取最接近用户地点的10条数据 [分页].
        bmobQuery.setSkip(mSearchDataMode.mSkipPageNum); // 分页效果
        bmobQuery.include("usertt"); // 获取这个发布课程的老师.
        bmobQuery.findObjects(mContext, new FindListener<CoursePublish>() {

            @Override
            public void onSuccess(List<CoursePublish> pubList) {
                Log.w("获取附近课程成功:" + pubList.size());
                //
                if (pubList.size() > 0) {
                    if (updateState == DOWN_PULL_STATE_UPDAE) { // 下拉刷新.
                        mPubList = pubList;
                    } else { // 上啦加载更多.
                        mPubList.addAll(pubList);
                    }
                    mSearchDataMode.mSkipPageNum += pubList.size();
                    mNearbyInfoAdpter.setPubList(mPubList);
                } else {
                    OpenToast.showMsg("附近课程已无");
                    if (updateState == DOWN_PULL_STATE_UPDAE) { // 下拉刷新.
                        mPubList.clear();
                        mNearbyInfoAdpter.setPubList(mPubList);
                    }
                }
                // 刷新.
                if (updateState == DOWN_PULL_STATE_UPDAE) {
                    mPullRefreshListView.onRefreshComplete();
                } else {
                    mPullRefreshListView.onLoadComplete();
                }
                mPullRefreshListView.setResultSize(pubList.size());
                setFirstLoadDataViewShow();
            }

            @Override
            public void onError(int code, String msg) {
                Log.e("code:" + code + " msg:" + msg);
                OpenToast.showMsg("获取附近课程失败");
                // 刷新.
                if (updateState == DOWN_PULL_STATE_UPDAE) {
                    mPullRefreshListView.onRefreshComplete();
                } else {
                    mPullRefreshListView.onLoadComplete();
                }
                setFirstLoadDataViewShow();
            }
        });
    }

    private boolean isFirst = true;

    private void setFirstLoadDataViewShow() {
        if (isFirst) {
            mProgressBar.setVisibility(View.GONE);
            mPullRefreshListView.setVisibility(View.VISIBLE);
            isFirst = false;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        mBaiDuLbsHelper.mLocationClient.stop();
    }

    /**
     * 第一次定位更新，获取附近数据.
     */
    public void onEvent(BDLocation location) {
        // 精度和维度.
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        // 详细地址.
        String lbsAddr = location.getAddrStr();
        //
        String province = location.getProvince(); // 比如: 四川省
        String city = location.getCity(); // 成都市
        String district = location.getDistrict(); // 武侯区
        //
        BmobGeoPoint tempBmobGeoPoint = new BmobGeoPoint(longitude, latitude);
        mSearchDataMode.bmobGeoPoint = tempBmobGeoPoint;
        restartUpdateData(DOWN_PULL_STATE_UPDAE);
    }

    /*
     *  附近搜索的关键字类.
     */
    public class SearchDataMode {
        public double sortType = MENU_NEW_FLAGS;
        public int oneId = -1; // 一级分类ID.
        public int twoId = -1; // 二级分类ID.
        public int threeId = -1; // 三级分类ID.
        public double kmRadio = 0.5; // 0.5[500米]，1[1公里]依次类推... ...(3km,5km)
        public int typeState = -1; // 类型.
        public int mSkipPageNum = 0; // 翻页.
        public BmobGeoPoint bmobGeoPoint;

        public String typeText = "教学生";
        public String sortText = "最新";
        public String oneText = "";
        public String twoText = "全部";
        public String threeText = "";
        public String kmText = "500m";


        @Override
        public String toString() {
            String location = "";
            if (bmobGeoPoint != null)
                location += bmobGeoPoint.getLongitude() + ":"
                        + bmobGeoPoint.getLatitude();
            return "oneId:" + oneId + " twoId:" + twoId + " threeId:" + threeId
                    + " location:" + location + " kmRadio:" + kmRadio + " mSkipPageNum:" + mSkipPageNum;
        }
    }

}
